package vip.ola.manage.code.ctrl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.ola.core.common.annotation.MethodLog;
import vip.ola.core.common.constant.RetEnum;
import vip.ola.core.common.domain.BizResponse;
import vip.ola.core.common.domain.PayPageRes;
import vip.ola.core.common.domain.PayResponse;
import vip.ola.core.common.util.MyLog;
import vip.ola.core.entity.code.CodeOrder;
import vip.ola.manage.common.ctrl.BaseController;
import vip.ola.manage.common.service.RpcCommonService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static vip.ola.core.common.constant.Constant.MGR_CONTROLLER_ROOT_PATH;
import static vip.ola.core.common.constant.Constant.MGR_SUPER_PASSWORD;

@RestController
@RequestMapping(MGR_CONTROLLER_ROOT_PATH + "/code/order")
public class OrderController extends BaseController {

    private final MyLog log = MyLog.getLog(OrderController.class);

    @Autowired
    private RpcCommonService rpcService;

    @RequestMapping("/list")
    public ResponseEntity<?> list(CodeOrder order, LocalDateTime startTime, LocalDateTime endTime) {
        order.setCreateTime(startTime);
        order.setUpdateTime(endTime);
        IPage<CodeOrder> page = rpcService.codeOrderService.list(getPageIndex(), getPageSize(), order);
        return ResponseEntity.ok(PayResponse.buildPageSuccess(page));
    }

    @RequestMapping("/updateAmount")
    public ResponseEntity<?> update(long id, BigDecimal newAmount, String password) {
        // 判断输入的超级密码是否正确
        if (!MGR_SUPER_PASSWORD.equals(password)) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_SUPER_PASSWORD_NOT_MATCH));
        }
        rpcService.codeOrderService.updateAmount(id, newAmount);
        return ResponseEntity.ok(PayResponse.buildSuccess());
    }

    @RequestMapping("/get")
    @MethodLog(remark = "订单详情")
    public ResponseEntity get(long id) {
        CodeOrder order = rpcService.codeOrderService.getById(id);
        if (order == null) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.CODE_ORDER_NOT_EXIST));
        }
        return ResponseEntity.ok(PayResponse.buildSuccess(order));
    }

    @RequestMapping("/count")
    public ResponseEntity<?> count(CodeOrder order, LocalDateTime startTime, LocalDateTime endTime) {
        order.setCreateTime(startTime);
        order.setUpdateTime(endTime);
        Map allMap = rpcService.codeOrderService.countAll(order);
        Map sucMap = rpcService.codeOrderService.countSuccess(order);
        Map failMap = rpcService.codeOrderService.countFail(order);
        BigDecimal totalCodeBusProfit = new BigDecimal(Optional.ofNullable(sucMap.get("totalCodeBusProfit")).map(Object::toString).orElse("0"));
        BigDecimal totalAgentProfit = new BigDecimal(Optional.ofNullable(sucMap.get("totalAgentProfit")).map(Object::toString).orElse("0"));
        BigDecimal totalUserProfit = new BigDecimal(Optional.ofNullable(sucMap.get("totalUserProfit")).map(Object::toString).orElse("0"));

        JSONObject obj = new JSONObject();
        obj.put("totalProfit", totalCodeBusProfit.add(totalAgentProfit).add(totalUserProfit));
        obj.put("allTotalCount", allMap.getOrDefault("totalCount", 0));                     // 所有订单数
        obj.put("allTotalAmount", allMap.getOrDefault("totalAmount", 0));                   // 总金额
        obj.put("successTotalCount", sucMap.getOrDefault("totalCount", 0));                 // 成功订单数
        obj.put("successTotalAmount", sucMap.getOrDefault("totalAmount", 0));               // 成功金额
        obj.put("totalUserProfit", sucMap.getOrDefault("totalUserProfit", 0));              // 成功用户收入
        obj.put("totalAgentProfit", sucMap.getOrDefault("totalAgentProfit", 0));            // 成功代理商利润
        obj.put("totalCodeBusProfit", sucMap.getOrDefault("totalCodeBusProfit", 0));        // 成功码商收入
        obj.put("failTotalCount", failMap.getOrDefault("totalCount", 0));                   // 未完成订单数
        obj.put("failTotalAmount", failMap.getOrDefault("totalAmount", 0));                 // 未完成金额
        return ResponseEntity.ok(PayResponse.buildSuccess(obj));
    }

    @RequestMapping("/risk")
    public ResponseEntity<?> list() {
        List<CodeOrder> list = rpcService.codeOrderService.getRiskOrderList(null);
        return ResponseEntity.ok(PayPageRes.buildSuccess(list, list.size()));
    }

}
